var e = getApp(),
    o = require("../../../utils/core");

Page({
    data: {
        jg: null,
        show:true,
        tabid: 0,
        alllist: [],
        sxval: '',
        list: [],
        isFilterShow: !1,
        screenlist: [],
        currentTab: [false, false, false, false, false, false],
        select: null,
        openid: null,
        selectid: null
    },
    onLoad: function (el) {
        var that = this
        e.checkAuth();
        var openid = wx.getStorageSync("openid")
        that.setData({
            openid: openid
        })
        o.post("goods.get_category", {
            openid: that.data.openid
        }, ((e) => {
            that.setData({
                screenlist: e.allcategory.parent
            })
        }))
        o.get("wxAppSetting", {}, function (e) {
            var o = e.sysset;
            that.setData({
                shop_logo: o.shoplogo,
                shop_name: o.shopname
            });
        });
        //获取一折购后台数据
        o.post("goods.yizhegou", {
            openid: that.data.openid,
            zone_type: 7
        }, ((e) => {
            that.setData({
                list: e,
                alllist: JSON.parse(JSON.stringify(e)) //深拷贝  修改list 不然会一起改变
            })
        }))
    },
    change_active(e) {
        var that = this
        //当前currentTab 数组赋值给arr 
        var arr = that.data.currentTab
        //当前选中下标
        var cur = e.currentTarget.dataset.current
        //如果当前选中和点击相同就取消
        arr[cur] = !arr[cur]
        that.setData({
            currentTab: arr
        })
        console.log(arr);
    },
    confirm() {
        var that = this
        o.post("goods.yizhegou", {
            isrecommand: that.data.currentTab[0] ? 1 : null,
            isnew: that.data.currentTab[1] ? 1 : null,
            ishot: that.data.currentTab[2] ? 1 : null,
            isdiscount: that.data.currentTab[3] ? 1 : null,
            issendfree: that.data.currentTab[4] ? 1 : null,
            istime: that.data.currentTab[5] ? 1 : null,
            openid: that.data.openid,
            zone_type: 7,
            id: that.data.selectid
        }, ((e) => {
            that.setData({
                list: e,
                tabid: 3,
                jg: null
            })
        }))
    },
    select_change(e) {
        var that = this
        var id = e.currentTarget.dataset.selectid
        var cur = e.currentTarget.dataset.select
        if (that.data.select == cur) {
            that.setData({
                select: null,
                selectid:null
            })
        } else {
            that.setData({
                select: cur,
                selectid: id
            })
        }
    },
    clear_active() {
        var that = this
        that.setData({
            tabid: null,
            jg: null,
            tabid:0,
            currentTab: [false, false, false, false, false, false]
        })
    },
    showFilter: function () {
        this.setData({
            isFilterShow: !this.data.isFilterShow,
            
        });
    },
    chengtab(e) {
        let tabid = e.currentTarget.dataset.id
        this.setData({
            tabid: tabid
        })
        if (tabid == 0) {
            var that = this
            that.setData({
                list: JSON.parse(JSON.stringify(this.data.alllist)), //深拷贝
                jg: null
            })
        } else if (tabid == 1) {
            var that = this
            var list = that.data.list
            var arr = list.list.sort(that.paixu('salesreal', false));
            list.list = arr
            that.setData({
                jg: null,
                list: list
            })
        } else if (tabid == 2) {
            let that = this
            var list = that.data.list
            var arr = list.list.sort(that.paixu('minprice', that.data.jg == 0));
            list.list = arr
            that.setData({
                jg: that.data.jg == 0 ? 1 : 0,
                list: list
            })
        }
    },
    // attr 需要验证的字符   rev   判定条件
    paixu: function (attr, rev) {
        //第二个参数没有传递 默认升序排列
        if (rev == undefined) {
            rev = 1;
        } else {
            rev = (rev) ? 1 : -1;
        }
        //商品升序或者降序
        return function (a, b) {
            a = a[attr];
            b = b[attr];
            if (a < b) {
                return rev * -1;
            }
            if (a > b) {
                return rev * 1;
            }
            return 0;
        }
    },
});